<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" inline :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
    <el-form-item label="基地" prop="deptId">
      <el-input v-model="dataForm.deptId" placeholder="基地"></el-input>
    </el-form-item>
    <el-form-item label="区域名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="区域名称"></el-input>
    </el-form-item>
    <el-form-item label="区域位置" prop="address">
      <el-input v-model="dataForm.address" placeholder="区域位置"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="现场图片路径" prop="liveImage">
      <div>
        <singleFileUpload></singleFileUpload>
        <!--<el-input v-model="dataForm.liveImage" placeholder="现场图片路径"></el-input>-->
      </div>
    </el-form-item>
    <el-form-item label="田块数量" prop="num">
      <el-input v-model="dataForm.num" placeholder="田块数量"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import singleFileUpload from '@/components/single-file-upload/index'
  export default {
    components: {
      singleFileUpload
    },
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          deptId: '',
          name: '',
          address: '',
          remark: '',
          liveImage: '',
          num: '',
   
        },
        dataRule: {
          deptId: [
            { required: true, message: '部门（基地）id不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '区域名称不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '区域位置不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          liveImage: [
            { required: true, message: '现场图片路径不能为空', trigger: 'blur' }
          ],
          num: [
            { required: true, message: '田块数量不能为空', trigger: 'blur' }
          ],
          createId: [
            { required: true, message: '创建人id不能为空', trigger: 'blur' }
          ],
          createName: [
            { required: true, message: '创建用户姓名不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateId: [
            { required: true, message: '修改人id不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          updateName: [
            { required: true, message: '修改用户姓名不能为空', trigger: 'blur' }
          ],
          version: [
            { required: true, message: '版本号不能为空', trigger: 'blur' }
          ],
          gcFlag: [
            { required: true, message: '删除标识 -1：删除 0：正常不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/buss/bussfarmlandarea/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.deptId = data.bussFarmlandArea.deptId
                this.dataForm.name = data.bussFarmlandArea.name
                this.dataForm.address = data.bussFarmlandArea.address
                this.dataForm.remark = data.bussFarmlandArea.remark
                this.dataForm.liveImage = data.bussFarmlandArea.liveImage
                this.dataForm.num = data.bussFarmlandArea.num
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            // 这里取上传后返回的数据
            console.log(this.$cookie.get('uploadFileId'))

            this.$http({
              url: this.$http.adornUrl(`/buss/bussfarmlandarea/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'deptId': this.dataForm.deptId,
                'name': this.dataForm.name,
                'address': this.dataForm.address,
                'remark': this.dataForm.remark,
                'liveImage': this.dataForm.liveImage,
                'num': this.dataForm.num,
                'createId': this.dataForm.createId,
                'createName': this.dataForm.createName,
                'createTime': this.dataForm.createTime,
                'updateId': this.dataForm.updateId,
                'updateTime': this.dataForm.updateTime,
                'updateName': this.dataForm.updateName,
                'version': this.dataForm.version,
                'gcFlag': this.dataForm.gcFlag
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
                // 请求完成后 删除上传的id
                this.$cookie.delete('uploadFileId')
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
